package com.devframe.srtp.server.file;

import java.io.File;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.devframe.srtp.Account;
import com.devframe.srtp.SRTPConstants;
import com.devframe.srtp.SRTPResult;
import com.devframe.srtp.server.parse.SRTPParam;
import com.devframe.srtp.util.common.SRTPThumbUtil;
import com.devframe.srtp.util.enumobj.EnumSRTPAction;

public class SRTPCreateThumbPictureHandler {

	private static final Log log=LogFactory.getLog(SRTPCreateThumbPictureHandler.class);
	
	private Account account;
	
	
	public SRTPCreateThumbPictureHandler(Account account){
		this.account=account;
		//路径保存到根路径下
	}
	public SRTPResult process(SRTPParam reqParams){
		SRTPResult result=new SRTPResult();
		result.setCode(false);
		result.setAction(EnumSRTPAction.CREATE_THUMB_PICTURE.getValue());
		
		if(StringUtils.isNotBlank(reqParams.getFilePath())){
			//真实路径
			String realPath=this.account.getRootPath()+reqParams.getFilePath();
			if(log.isDebugEnabled()){
				log.debug("生成缩略图："+realPath);
			}
			File file=new File(realPath);
			//必须是文件的
			boolean bool=false;
			if(file.exists()){
				if(log.isDebugEnabled()){
					log.debug("生成缩略图");
				}
				String thumbFilePath=SRTPThumbUtil.getThumbImagePath(realPath);
				File thumbFile=new File(thumbFilePath);
				if(!thumbFile.exists()){//缩略图不存在的情况下
					//生成缩略图
					SRTPThumbUtil.createThumbImage(new File(realPath),thumbFile,this.account.getThumbWidth(),this.account.getThumbHeight());
					result.setCode(bool);
					result.setMsg("缩略图创建成功");
				}else{
					result.setCode(bool);
					result.setMsg("缩略图已存在，无法创建；缩略图路径="+thumbFilePath);
				}
			}
			
		}
		if(!result.isCode()){
			result.setMsg("缩略图创建失败");
		}
		return  result;
	}
}
